//
//  SettingViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "SettingViewController.h"
#import "Masonry.h"
#import "SVProgressHUD.h"
#import "BBZSuggestionViewController.h"
#import "BBZModifyViewController.h"

/**
 *  BBZ zhouyang
 */
@interface SettingViewController () <UITableViewDelegate, UITableViewDataSource>

//添加tableView
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *titleArray;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置初始化界面
    [self setupUI];
}

#pragma mark -- 初始化界面
- (void)setupUI {
    
    //关闭导航栏translucent属性
    self.navigationController.navigationBar.translucent = NO;
    
    //添加tableview
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.bottom.equalTo(self.view);
        make.edges.equalTo(self.view);
    }];
    
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.titleArray = @[@"意见反馈",@"加号符号说明",@"修改密码",@"注销用户"];
    
    self.tableView.tableFooterView = [UIView new];
}

#pragma mark -- 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.textLabel.text = self.titleArray[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//cell的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(indexPath.row == 0) {
        //跳转到意见反馈的界面
        BBZSuggestionViewController *suggestionVC = [[BBZSuggestionViewController alloc] init];
        
        [self.navigationController pushViewController:suggestionVC animated:YES];
        
        return;
    }
    
    if(indexPath.row == 2) {
        
        //跳转到修改密码界面
        BBZModifyViewController *modifyVC = [[BBZModifyViewController alloc] init];
        
        [self.navigationController pushViewController:modifyVC animated:YES];
    }
    
    if (indexPath.row == 3) {
        
        //创建注销的弹框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"你确定要注销吗?" preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            //调用注销方法
            [self logout];
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        [alert addAction:sureAction];
        [alert addAction:cancelAction];
        
        [self presentViewController:alert animated:YES completion:nil];
        
        return;
    }
}

#pragma mark -- 注销调用的方法
- (void)logout {
    
    //使用通知传值
    NSDictionary *dict = @{@"isLogin":@"0"};
    NSNotification *n = [[NSNotification alloc]initWithName:@"isLoginNotificationKey" object:nil userInfo:dict];
    
    [[NSNotificationCenter defaultCenter] postNotification:n];
    
    //返回主界面，显示弹框
    [self.navigationController popViewControllerAnimated:YES];
    
    //延时0.2秒出现弹框
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showSuccessWithStatus:@"注销成功" ];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
        });
    });
    
    
}


//懒加载
- (UITableView *)tableView {
    
    if(nil == _tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}


@end
















